package com.zst.amqp.entity;

import com.zst.amqp.utils.StringUtils;

import java.util.*;

/**
 * ConsumerInfo的上下文
 */
public class ConsumerInfoContext {
    private final Map<String, List<ConsumerInfo>> context = new HashMap<>();

    /**
     * 注册消费者信息
     * @param consumerInfo
     * @throws Exception
     */
    public void register(ConsumerInfo consumerInfo) throws Exception {
        String msgType = consumerInfo.getMsgType();
        if (StringUtils.isEmpty(msgType)) {
            throw new Exception("Message type cannot be empty");
        }

        List<ConsumerInfo> consumerInfos = context.get(msgType);
        if (consumerInfos == null) {
            consumerInfos = new ArrayList<>();
            context.put(msgType, consumerInfos);
        }

        consumerInfos.add(consumerInfo);
    }

    /**
     * 获取某个消息类型下的消费者列表
     * @param msgType
     * @return
     */
    public List<ConsumerInfo> getConsumerInfoList(String msgType) {
        return context.get(msgType);
    }

    /**
     * 获取消息类型列表
     * @return
     */
    public Set<String> getConsumerMsgTypeSet() {
        return this.context.keySet();
    }
}
